package com.moore.attendance.boot;

import android.content.Context;
import android.content.Intent;

/**
 * Created by MooreLi on 2017/6/5.
 */

public class BootUtil {
    private static final String BOOT_BROADCAST = "com.example.jt.boottime";
    private static final String SHUT_DOWN_BROADCAST = "com.example.jt.shutdowntime";

    /**
     * 定时开机
     *
     * @param context
     * @param hourOn
     * @param minuteOn
     */
    public static void setPowerOn(Context context, int hourOn, int minuteOn, int yearOn, int monthOn, int dayOn) {
        if (hourOn >= 0 && hourOn < 24 && minuteOn >= 0 && minuteOn < 60 && yearOn > 0 && monthOn > 0 && monthOn <= 12 && dayOn > 0) {
            String bootTime = "1,0,0," + hourOn + "," + minuteOn + "," + yearOn + "," + monthOn + "," + dayOn;
            Intent bootIntent = new Intent(BOOT_BROADCAST);
            bootIntent.putExtra("message", bootTime);
            context.sendBroadcast(bootIntent);
        }
    }

    public static void setPowerOn(Context context, int hourOn, int minuteOn) {
        if (hourOn >= 0 && hourOn < 24 && minuteOn >= 0 && minuteOn < 60) {
            String bootTime = "1,0,0," + hourOn + "," + minuteOn;
            Intent bootIntent = new Intent(BOOT_BROADCAST);
            bootIntent.putExtra("message", bootTime);
            context.sendBroadcast(bootIntent);
        }
    }

    /**
     * 取消定时开机
     *
     * @param context
     */
    public static void cancelPowerOn(Context context) {
        Intent bootIntent = new Intent(BOOT_BROADCAST);
//        bootIntent.putExtra("message", "0,0,0,0,0,0,0,0");
        bootIntent.putExtra("message", "0,0,0,0,0");
        context.sendBroadcast(bootIntent);
    }

    /**
     * 定时关机
     *
     * @param context
     * @param hourOff
     * @param minuteOff
     */
    public static void setPowerOff(Context context, int hourOff, int minuteOff) {
        if (hourOff >= 0 && hourOff < 24 && minuteOff >= 0 && minuteOff < 60) {
            String shutDownTime = "1,0,0," + hourOff + "," + minuteOff;
            Intent shutDownIntent = new Intent(SHUT_DOWN_BROADCAST);
            shutDownIntent.putExtra("message", shutDownTime);
            context.sendBroadcast(shutDownIntent);
        }
    }

    /**
     * 取消定时关机
     *
     * @param context
     */
    public static void cancelPowerOff(Context context) {
        Intent shutDownIntent = new Intent(SHUT_DOWN_BROADCAST);
        shutDownIntent.putExtra("message", "cancel");
        context.sendBroadcast(shutDownIntent);
    }

    /**
     * 关机
     *
     * @param context
     */
    public static void shutDown(Context context) {
        context.sendBroadcast(new Intent().setAction("android.intent.action.shutdown"));
    }

    /**
     * 重启
     *
     * @param context
     */
    public static void restart(Context context) {
        context.sendBroadcast(new Intent().setAction("android.intent.action.pubds_reboot"));
    }

    /**
     * 启动看门狗
     *
     * @param context
     */
    public static void startWatchDog(Context context) {
        context.sendBroadcast(new Intent().setAction("android.intent.action.pubds_watchdogenable"));
    }

    /**
     * 关闭看门狗
     *
     * @param context
     */
    public static void stopWatchDog(Context context) {
        context.sendBroadcast(new Intent().setAction("android.intent.action.pubds_watchdogenable"));
    }

    /**
     * 关闭屏幕
     *
     * @param context
     */
    public static void closeScreen(Context context) {
        context.sendBroadcast(new Intent().setAction("android.intent.action.pubds_sleep"));
    }

    /**
     * 开启屏幕（调用重启接口）
     *
     * @param context
     */
    public static void openScreen(Context context) {
        restart(context);
    }
}
